package com.learn.elasticsearch.controller;


import cn.hutool.json.JSONUtil;
import co.elastic.clients.elasticsearch.indices.IndexState;
import com.google.common.collect.Maps;
import com.learn.elasticsearch.domain.BookBean;
import com.learn.elasticsearch.service.EsSearchService;
import com.mate.cloud.core.response.BaseResponse;
import com.mate.cloud.core.web.controller.AdminBaseController;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Map;

/**
 * es 操作
 * @author:         MI
 * @email:          448341911@qq.com
 * @createTime:     2024/1/15 16:48
 * @updateUser:     MI
 * @updateTime:     2024/1/15 16:48
 * @updateRemark:   修改内容
 * @version:        v1.0
 */
@Slf4j
@RestController
public class ElasticController extends AdminBaseController {


    @Resource
    private EsSearchService esearchService;

    @GetMapping("/createIndex/{indexName}")
    public BaseResponse createIndex(@PathVariable("indexName") String indexName) {
        log.info("=============== ElasticController#createIndex==============={}", JSONUtil.toJsonStr(indexName));
        return successBodyResponse(esearchService.createIndex(indexName));
    }
    @GetMapping("/book/{id}")
    public BaseResponse load(@PathVariable("id") String id) {

        return successBodyResponse(null);
    }

    /**
     * 判断索引是否存在
     * @param indexName
     * @return
     */
    @GetMapping("/existsIndex/{indexName}")
    public BaseResponse existsIndex(@PathVariable("indexName") String indexName) throws IOException {
        log.info("=============== ElasticController#existsIndex==============={}", indexName);
        boolean existsed = esearchService.hasIndex(indexName);
        return successBodyResponse(existsed);
    }

    /**
     * 查询索引详情
     * @param indexName
     * @return
     */
    @GetMapping("/queryIndex/{indexName}")
    public BaseResponse queryIndex(@PathVariable("indexName") String indexName) {
        log.info("=============== ElasticController#queryIndex==============={}", indexName);
        Map<String, IndexState> indexStateMap = esearchService.queryIndex(indexName);
        return successBodyResponse(indexStateMap);
    }

    /**
     * 新增数据
     *
     * @param indexName
     * @param id        自己的主键标识
     * @throws IOException
     */
    @PostMapping("/save/{indexName}")
    public BaseResponse saveDocument(String indexName) {

        Map<String, Object> obj = Maps.newHashMap();
        BookBean bookBean = BookBean.builder()
                .id("123")
                .title("Spring Boot 实战")
                .author("MI")
                .build();
        obj.put("bookBean", bookBean);

        String indexId = esearchService.insertDocument(indexName, obj, indexName);
        return successBodyResponse(indexId);
    }
}
